package com.voute.View.Fragement;


import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.Base.BaseFragment;
import com.voute.Model.Bean.ResourceDetailsInfoBean;
import com.voute.Presenter.IUpdatePresenter;
import com.voute.Presenter.Impl.UpdatePresenterImpl;
import com.voute.View.Adapter.UpdateAdapter;
import com.voute.View.IView.IUpdateView;

import java.util.ArrayList;
import java.util.List;

/**
 * 今日更新Fragment
 */
public class UpdateFragment extends BaseFragment implements IUpdateView {

    private IUpdatePresenter updatePresenter;
    private SwipeRefreshLayout swipe_refresh;
    private ListView content_list;
    private UpdateAdapter adapter;
    private ArrayList<ResourceDetailsInfoBean> datas;
    private RelativeLayout no_data;
    private ImageView no_data_iv;

    @Override
    public int getLayoutId() {
        return 0;//R.layout.fragment_hot;
    }

    @Override
    public void setupView() {
//        swipe_refresh = (SwipeRefreshLayout) $(R.id.swipe_refresh);
//        swipe_refresh.setColorSchemeColors(Color.parseColor("#0f88eb"));
//        content_list = (ListView) $(R.id.content_list);
//        no_data=(RelativeLayout)$(R.id.no_data);
//        no_data_iv=(ImageView)$(R.id.no_data_iv);
//        datas = new ArrayList<>();
//        adapter = new UpdateAdapter(getActivity(), datas);
//        content_list.setAdapter(adapter);
//        no_data_iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                no_data.setVisibility(View.GONE);
//                swipe_refresh.setVisibility(View.VISIBLE);
//                setupData();
//            }
//        });
//        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                setupData();
//            }
//        });
    }

    @Override
    public void setupData() {
        updatePresenter = new UpdatePresenterImpl(this);
        updatePresenter.getUpdateToday();
    }

    @Override
    public void showLoading() {
        swipe_refresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipe_refresh.setRefreshing(false);
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(swipe_refresh,errMsg, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(getActivity()).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    @Override
    public void setUpdates(List<ResourceDetailsInfoBean> datas) {
        if (datas != null) {
            adapter.setDatas(datas);
            if(datas.size()==0){
                Snackbar.make(swipe_refresh,"暂时未有更新", Snackbar.LENGTH_SHORT).show();
                no_data.setVisibility(View.VISIBLE);
                swipe_refresh.setVisibility(View.GONE);
            }
            else{
                no_data.setVisibility(View.GONE);
                swipe_refresh.setVisibility(View.VISIBLE);
            }
        }
    }
}
